<template>
    <div class="sortable-list">
        <div class="list-header">
            <h4 class="list-title">{{ title }}</h4>
        </div>

        <div class="list-content">
            <div v-for="(item, index) in sortedItems" :key="item.id || index" class="list-item">
                <div class="item-icon">
                    <el-icon v-if="item.icon" :size="20">
                        <component :is="item.icon" />
                    </el-icon>
                    <img v-else-if="item.iconUrl" :src="item.iconUrl" :alt="item.name" class="custom-icon" />
                    <div v-else class="default-icon">
                        {{ item.name?.charAt(0)?.toUpperCase() }}
                    </div>
                </div>

                <div class="item-info">
                    <div class="item-name">{{ item.name }}: {{ getPercentage(item.value) }}%</div>
                    <div class="percentage-bar">
                        <div class="percentage-fill" :style="{ width: getPercentage(item.value) + '%' }"></div>
                    </div>
                </div>

                <div class="item-count">
                    {{ item.count || 0 }}次
                </div>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { computed, ref } from 'vue'

// 定义组件属性
interface Props {
    // 列表标题
    title: string
    // 列表数据
    items: Array<{
        id?: string | number
        name: string
        value: number
        description?: string
        icon?: string
        iconUrl?: string
        unit?: string
        count?: number
    }>
    // 默认排序方式
    defaultSort?: 'asc' | 'desc'
    // 最大显示数量
    maxItems?: number
}

// 设置默认值
const props = withDefaults(defineProps<Props>(), {
    defaultSort: 'desc',
    maxItems: 10
})

// 排序状态
const sortOrder = ref(props.defaultSort)

// 排序后的数据
const sortedItems = computed(() => {
    let sorted = [...props.items]

    // 按值排序
    if (sortOrder.value === 'asc') {
        sorted.sort((a, b) => a.value - b.value)
    } else {
        sorted.sort((a, b) => b.value - a.value)
    }

    // 限制显示数量
    return sorted.slice(0, props.maxItems)
})

// 计算百分比
const getPercentage = (value: number) => {
    if (props.items.length === 0) return 0
    const maxValue = Math.max(...props.items.map(item => item.value))
    return maxValue > 0 ? Math.round((value / maxValue) * 100) : 0
}
</script>

<style scoped>
.sortable-list {
    background: #fff;
    border-radius: 8px;
    border: 1px solid #e9ecef;
    overflow: hidden;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.list-header {
    padding: 16px 20px;
    background: #fff;
    border-bottom: 1px solid #e9ecef;
}

.list-title {
    margin: 0;
    font-size: 16px;
    font-weight: 600;
    color: #333;
}

.list-content {
    padding: 0;
}

.list-item {
    display: flex;
    align-items: center;
    padding: 16px 20px;
    border-bottom: 1px solid #f0f0f0;
    transition: all 0.2s ease;
}

.list-item:hover {
    background: #f8f9fa;
}

.list-item:last-child {
    border-bottom: none;
}

.item-icon {
    width: 32px;
    height: 32px;
    margin-right: 16px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 6px;
    background: #f8f9fa;
    color: #666;
}

.custom-icon {
    width: 24px;
    height: 24px;
    object-fit: contain;
    border-radius: 4px;
}

.default-icon {
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    font-weight: 600;
}

.item-info {
    flex: 1;
    margin-right: 16px;
    min-width: 0;
}

.item-name {
    font-size: 14px;
    font-weight: 500;
    color: #333;
    margin-bottom: 8px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.percentage-bar {
    width: 100%;
    height: 6px;
    background: #f0f0f0;
    border-radius: 3px;
    overflow: hidden;
}

.percentage-fill {
    height: 100%;
    background: #409eff;
    border-radius: 3px;
    transition: width 0.3s ease;
}

.item-count {
    font-size: 14px;
    color: #333;
    font-weight: 500;
    min-width: 40px;
    text-align: right;
}

/* 响应式设计 */
@media (max-width: 768px) {
    .list-item {
        padding: 12px 16px;
    }

    .item-icon {
        width: 28px;
        height: 28px;
        margin-right: 12px;
    }

    .item-info {
        margin-right: 12px;
    }

    .item-count {
        min-width: 35px;
    }
}

@media (max-width: 480px) {
    .list-item {
        flex-wrap: wrap;
        gap: 8px;
    }

    .item-count {
        width: 100%;
        text-align: left;
        margin-top: 8px;
    }
}
</style>
